package com.example.controller;

import com.example.common.response.Result;
import com.example.model.entity.Post;
import com.example.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/posts")
public class PostController {
    @Autowired
    private PostService postService;

    @PostMapping
    public Result<Post> createPost(@RequestBody Post post, @RequestParam Long userId) {
        return Result.success(postService.createPost(post, userId));
    }

    @GetMapping("/{id}")
    public Result<Post> getPostById(@PathVariable Long id) {
        return Result.success(postService.findById(id));
    }

    @GetMapping("/user/{userId}")
    public Result<List<Post>> getPostsByUserId(@PathVariable Long userId) {
        return Result.success(postService.findByUserId(userId));
    }

    @PutMapping("/{id}")
    public Result<Post> updatePost(@PathVariable Long id, @RequestBody Post post) {
        return Result.success(postService.updatePost(id, post));
    }

    @DeleteMapping("/{id}")
    public Result<Void> deletePost(@PathVariable Long id) {
        postService.deletePost(id);
        return Result.success(null);
    }
} 